<?php
// Include the model definition you want to overload if needed
require_once('model/Parameter.php');
// Extends from the model class
class Parameter extends DataObjects_Parameter
{
	//parameter array
	//Usage outside class:	Parameter::$params['PARAMETER_NAME']
	public static $params = array();
	
	const MAX_LENGTH_NAME = 64;
	const MAX_LENGTH_CODE = 15;
	const MAX_LENGTH_DESC = 512;
	const MAX_LENGTH_URL  = 255;
	const MAX_LENGTH_PASSWORD=64;
    const MIN_LENGTH_PASSWORD=6;
	const MAX_LENGTH_DATE = 10;
	const MAX_LENGTH_NUMBER = 10;
	const MAX_LENGTH_NUMBER_SMALL = 5;
	const DATE_FORMAT = 'dd/mm/yyyy';
	const DATE_MYSQL_FORMAT = '%Y-%m-%d';
	const DATE_FORMAT_DISPLAY = '%d/%m/%Y';
	const DATE_PHP_MYSQL_FORMAT = 'Y-m-d';
	
	const MAX_LENGTH_EMAIL = 100;

	const MAX_LENGTH_ADDRESS = 256;
	const MAX_LENGTH_TELEPHONE = 12;
	const MAX_LENGTH_TYPE = 1;
	//TA THEM//
	const MAX_LENGTH_PRICE = 10;
	//END TA THEM//
	// define size of interface
	const SIZE_COL_DESCRIPTION = 50;
	const SIZE_ROW_DESCRIPTION = 6;
	
	const SIZE_NAME = 20;
	const SIZE_CAPTION = 40;
	const SIZE_URL = 40;
	const SIZE_DATE = 10;
	const SIZE_NUMBER = 10;
	const SIZE_NUMBER_SMALL = 5;
	const SIZE_CONTACT_NAME = 30; // use for contact
	const SIZE_EMAIL = 30;
	const SIZE_PASSWORD = 30;
	const SIZE_CONFIRM_PASSWORD = 30;
	const SIZE_FIRST_NAME = 30;
	const SIZE_LAST_NAME = 30;
	
	
	// language
	const ENLISH_LANGUAGE ='en';	
	const VIETNAM_LANGUAGE ='vn';
	
	// param type 
	const PARAM_TYPE_NUMBER			 = 10;
	const PARAM_TYPE_BOOLEAN		 = 30;
	const PARAM_TYPE_DATETIME		 = 40;
	const PARAM_TYPE_POSITIVE_NUMBER = 50;
	const PARAM_TYPE_DOUBLE_NUMBER   = 60;
	const PARAM_TYPE_TEXT 			 = 70;
	const PARAM_TYPE_STRING 		 = 80;
	const PARAM_TYPE_PASSWORD 		 = 90;
	/**
	 * Load all the parameters from database
	 */
	public static function loadAllParameters()
	{	
		$param = new Parameter();
		$param->selectAdd();
		$param->selectAdd('type, name, value');
		$param->find();
		if ($param->N)
		{
			while ($param->fetch())
			{
				self::$params[$param->name] = $param->value;
			}
		}
	}	
	
	/**
	 * Get list parameter from database
	 */
	public function getListParameter()
	{
		$array_param = array();
		$this->selectAdd();
		$this->selectAdd('id, type, short_description as name, 
						description, value');
		$this->orderBy('id');
		$this->find();
		
		while($this->fetch())
		{	
			$array_param[] = array('id'  =>$this->id,
								   'type'=>$this->type,
								   'name'=>$this->name,
								   'description'=>$this->description,
								   'value'=>$this->value								   							
								  );
		}
		return $array_param;
	}
	
	/**
	 * Update parameter value
	 *
	 * @param  interger $id_param id of parameter
	 * @param  unknown_type $value input date or interger value
	 * @return boolean  return true if updating is success
	 */
	public function saveValue($id_param, $value)
     {
     	$param = new Parameter();
     	$param->get($id_param);
     	
    	if ($param->value != $value)
    	{
    		$param->query("UPDATE parameter set value = '$value' WHERE id = '$id_param'");
    		return true;
    	}
    	return false;
     }     
	 
	 public function saveValueName($name, $value)
     {
    	$p = new Parameter();
		
		$p->query("UPDATE parameter set value = '$value' WHERE name = '$name'");
     }     
}
